package com.easyedu.core.util;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.HashMap;


/**
 * @author liumengran
 */
@Data
@ToString
public class R<T> implements Serializable {

    private static Integer SUCCESS = 2000;
    private static Integer ERROR = 1999;

    private T data;

    private boolean success;

    private Integer code;

    private String message;

    private Integer errno;


    private R() {
    }


    public static R ok(){
        R r = new R<>();
        r.setCode(SUCCESS);
        r.setSuccess(true);
        r.setMessage("成功");
        r.setErrno(0);
        return r;
    }

    public static R error() {
        R r = new R<>();
        r.setSuccess(false);
        r.setCode(ERROR);
        r.setMessage("失败");
        r.setErrno(1);
        return r;
    }

    public static R error(int code) {
        R r = new R<>();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage("失败");
        r.setErrno(1);
        return r;
    }


    public R message(String message){
        this.setMessage(message);
        return this;
    }


    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R data(T data){
        this.data = data;
        return this;
    }

    public R data(String key, Object value){
        HashMap<String, Object> hs = new HashMap<>();
        hs.put(key, value);
        this.data = (T) hs;
        return this;
    }



}